package com.csu.device.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.csu.device.entity.Behavior;
import com.csu.device.entity.DeviceBinding;
import com.csu.device.entity.DeviceRela;
import com.csu.device.entity.SiteDO;
import com.csu.device.service.DeviveBindingService;
import com.csu.device.service.HardWareService;
import com.csu.util.PageUtils;
import com.csu.util.R;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin(origins="*")
@Controller
public class DeviceSimpleController {

    @Autowired
    private HardWareService hardWareService;

    @Autowired
    private DeviveBindingService deviveBindingService;

    @RequestMapping(value = "simple", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object mattress(@RequestBody JSONObject jsonParam) {
        return hardWareService.findAll();
    }

    /**
     * 查询列表数据
     *
     * @param params
     * @return
     */
    @ResponseBody
    @GetMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        return deviveBindingService.findDeviceBinding(params);
    }


    @ResponseBody
    @RequestMapping("/updateDevive")
    public R updateDevive(@RequestBody DeviceBinding deviceBinding) {

        if (deviveBindingService.updateDevive(deviceBinding) > 0) {
            return R.ok();
        } else {
           return R.error();
        }
    }

    @CrossOrigin(origins="*")
    @RequestMapping(value = "addDevive")
    @ResponseBody
    public R addDevive(@RequestBody DeviceBinding deviceBinding) {
        if (deviveBindingService.addDevive(deviceBinding) > 0) {
            return R.ok();
        } else {
            return R.error();
        }

    }

    @CrossOrigin(origins="*")
    @ResponseBody
    @RequestMapping("/deleteDevice")
    public R deleteDevice(@RequestBody DeviceBinding deviceBinding) {

        if (deviveBindingService.deleteDevice(deviceBinding) > 0) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     * 刷新redis缓存
     * @param params 参数id
     * @return
     */
    @ResponseBody
    @RequestMapping("/fushDevice")
    public R fushDevice(@RequestParam Map<String, String> params) {
        hardWareService.initDeviceData(params);
        return R.ok();
    }

    /**
     * 获取所有硬件设备
     * @param params
     * @return
     */
    @ResponseBody
    @RequestMapping("/hardware")
    public List<Map<String,Object>> findAllHardware(@RequestParam Map<String, String> params) {
        return  hardWareService.findAllHardware();
    }


    /**
     * 检验硬件编号是否重复
     * @param map
     * @return
     */
    @ResponseBody
    @PostMapping("/validateByDeviceSn")
    public String validateByDeviceSn(@RequestParam Map<String, Object> map) {
        PageUtils pageUtils= deviveBindingService.findDeviceBinding(map);
        if (pageUtils.getRows().size()!=0) {
            return "false";
        }
        return "true";
    }




    @CrossOrigin(origins="*")
    @ResponseBody
    @RequestMapping("/getDeviceBindingInfo/{id}")
    public Map<String,Object> getDeviceBindingInfo(@PathVariable("id") Integer id, Model model) {

        Map<String, Object> map=new HashMap<>();
        map.put("id",id);
        PageUtils pageUtils= deviveBindingService.findDeviceBinding(map);
        if (pageUtils.getRows().size()==1) {
            return (Map)pageUtils.getRows().get(0);
        }else{
             return null;
        }
    }


    @CrossOrigin(origins="*")
    @ResponseBody
    @RequestMapping("/findAllBehaviorToView/{userId}/{id}/{limit}")
    public List<Map<String,Object>> findAllBehaviorToView(@PathVariable("userId") String userId,@PathVariable("id") Integer id,@PathVariable("limit") Integer limit, Model model) {

        Map<String, Object> map=new HashMap<>();
        map.put("limit",limit);
        map.put("id",id);
        map.put("userId",userId);
       return deviveBindingService.findAllBehaviorToView(map);
    }


    /**
     * 异常列表数据查询
     * @param limit
     * @param model
     * @return
     */
    @CrossOrigin(origins="*")
    @ResponseBody
    @RequestMapping("/findAllBehaviorByError/{limit}")
    public List<Behavior> findAllBehaviorToView(@PathVariable("limit") Integer limit, Model model) {

        Map<String, Object> map=new HashMap<>();
        map.put("limit",limit);
        return deviveBindingService.findAllBehaviorByError(map);
    }


    /**
     * 开卡
     * @param userId
     * @param deviceSn
     * @param model
     * @return
     */
    @CrossOrigin(origins="*")
    @ResponseBody
    @RequestMapping("/bandCard/{userId}/{deviceSn}/{useName}")
    public R bandCard(@PathVariable("userId") String userId,@PathVariable("deviceSn") String deviceSn,@PathVariable("useName") String useName, Model model) {

        if (deviveBindingService.bandCard(userId,deviceSn,useName) > 0) {
            return R.ok();
        } else {
            return R.error();
        }

    }

    /**
     * 解绑
     * @param model
     * @return
     */
    @CrossOrigin(origins="*")
    @ResponseBody
    @RequestMapping("/unBandCard/{deviceSn}")
    public R unBandCard(@PathVariable("deviceSn") String deviceSn, Model model) {

        if(null==deviceSn && "".equals(deviceSn)){
            return R.error();
        }
        deviceSn=deviceSn.trim();
        if (deviveBindingService.unBandCard(deviceSn) > 0) {
            return R.ok();
        } else {
            return R.error();
        }
    }


    /**
     * 根据设备号查询历史记录
     * @param deviceSn
     * @param limit
     * @return
     */
    @CrossOrigin(origins="*")
    @ResponseBody
    @RequestMapping("/findAllBehaviorBySn/{deviceSn}/{limit}")
    public List<Map<String,Object>> findAllBehaviorBySn(@PathVariable("deviceSn") String deviceSn,@PathVariable("limit") Integer limit) {
        Map<String, Object> map=new HashMap<>();
        map.put("limit",limit);
        map.put("deviceSn",deviceSn);
        return deviveBindingService.findAllBehaviorByScene(map);
    }


    /**
     * 根据sceneId查询设备使用情况
     * @param sceneId
     * @param limit
     * @return
     */
    @CrossOrigin(origins="*")
    @ResponseBody
    @RequestMapping("/findAllBehaviorByScene/{sceneId}/{limit}")
    public List<Map<String,Object>> findAllBehaviorByScene(@PathVariable("sceneId") String sceneId,@PathVariable("limit") Integer limit) {
        Map<String, Object> map=new HashMap<>();
        map.put("limit",limit);
        map.put("sceneId",sceneId);
        return deviveBindingService.findAllBehaviorByScene(map);
    }


    /**
     * 根据设备类型查询
     * @param deviceType
     * @param limit
     * @return
     */
    @CrossOrigin(origins="*")
    @ResponseBody
    @RequestMapping("/findAllBehaviorByDeviceType/{deviceType}/{familyId}/{limit}")
    public List<Map<String,Object>> findAllBehaviorByDeviceType(@PathVariable("deviceType") String deviceType,@PathVariable("familyId") String familyId,@PathVariable("limit") Integer limit) {
        Map<String, Object> map=new HashMap<>();
        map.put("limit",limit);
        map.put("deviceType",deviceType);
        map.put("familyId",familyId);
        return deviveBindingService.findAllBehaviorByScene(map);
    }


    /**
     * 查询所有异常数据
     * @param params
     * @return
     */
    @CrossOrigin(origins="*")
    @ResponseBody
    @GetMapping("/findAllBehaviorByPage")
    public PageUtils findAllBehaviorByPage(@RequestParam Map<String, Object> params) {
        return deviveBindingService.findAllBehaviorByPage(params);
    }


    /**
     * 处理异常消息
     * @return
     */
    @CrossOrigin(origins="*")
    @ResponseBody
    @RequestMapping("/updateBehaviorByMessage/{id}")
    public R updateBehaviorByMessage(@PathVariable("id") Integer id) {
        Map<String, Object> map=new HashMap<>();
        map.put("id",id);
        if (deviveBindingService.updateBehaviorByMessage(map) > 0) {
            return R.ok();
        } else {
            return R.error();
        }
    }


    /**
     * 查询老人的绑定的设备号
     * @param deviceType
     * @param userId
     * @return
     */
    @CrossOrigin(origins="*")
    @ResponseBody
    @RequestMapping("/findDeviceSnByUserIdOrTypeId/{deviceType}/{userId}")
    public List<Map<String,Object>> findDeviceSnByUserIdOrTypeId(@PathVariable("deviceType") String deviceType,@PathVariable("userId") String userId) {
        Map<String, Object> map=new HashMap<>();

        map.put("deviceType",deviceType);
        map.put("userId",userId);
        return deviveBindingService.findDeviceSnByUserIdOrTypeId(map);
    }


    /**
     * 查询所有签约老人
     * @return
     */
    @CrossOrigin(origins="*")
    @ResponseBody
    @RequestMapping("/allUserId")
    public List<String> allUserId() {
        return deviveBindingService.allUserId();
    }


    /**
     * 查询3d效果图
     * @param vdate
     * @param familyId
     * @return
     */
    @CrossOrigin(origins = "*")
    @ResponseBody
    @RequestMapping("/findAllDevice/{vdate}/{familyId}")
    public JSONArray findAllDevice(@PathVariable("vdate") String vdate, @PathVariable("familyId") String familyId) {
        Map<String, Object> map = new HashMap<>();
        map.put("vdate", vdate);
        map.put("familyId", familyId);
        return deviveBindingService.findAllDevice(map);
    }


    /**
     * 保存电子围栏
     * @return
     */
    @CrossOrigin(origins = "*")
    @ResponseBody
    @RequestMapping("/fence/{userId}/{gid}")
    public R fence(@PathVariable("userId") String userId, @PathVariable("gid") String gid) {
        DeviceRela rela=new DeviceRela();
        rela.setName("电子围栏");
        rela.setDeviceBindingId(gid);
        return  R.ok();
    }


    /**
     * 删除电子围栏
     * @param userId
     * @return
     */
    @CrossOrigin(origins = "*")
    @ResponseBody
    @RequestMapping("/deleteByFence/{userId}/{gid}")
    public R deleteByFence(@PathVariable("userId") String userId) {

      return  R.ok();
    }


    /**
     * 查询异常分类
     * @return
     */
    @CrossOrigin(origins = "*")
    @ResponseBody
    @RequestMapping("/gTypeCount")
    public JSONArray gTypeCount() {

        return  deviveBindingService.findBtype();
    }
}
